#include<iostream>
#include<string>
#include<vector>
using namespace std;
//  Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
 
class Solution {
public:
    int sumNumbers(TreeNode* root) {
        if (root == NULL) {
            return 0;
        }
        else {
            int sum = 0;
            int T = PreTravel(root,sum);
            return T;
        }
    }
    int PreTravel(TreeNode* root,int sum) {
        if (root->left== NULL && root->right == NULL) {
            return sum+root->val;
        }
        sum = (sum + root->val)*10;
        int leftresult = 0;
        int rightresult = 0;
        if (root->left) {
            
            leftresult = PreTravel(root->left, sum);
        }
        if (root->right) {
            rightresult = PreTravel(root->right, sum);
        }
        return (leftresult + rightresult);
        

        
    }
};

//
//int main() {
//    TreeNode p1(4);
//    TreeNode p2(9);
//    TreeNode p3(0);
//    TreeNode p4(5);
//    TreeNode p5(1);
//    p1.left = &p2;
//    p1.right = &p3;
//    p2.left = &p4;
//    p2.right = &p5;
//    Solution S;
//    int val = S.sumNumbers(&p1);
//    cout << val << endl;
//}